package com.ruoyi.complex.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.apache.poi.sl.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.complex.constant.FormStatus;
import com.ruoyi.complex.domain.ComplexScoreForm;
import com.ruoyi.complex.dto.ExcelColumn;
import com.ruoyi.complex.service.IComplexScoreFormService;
import com.ruoyi.complex.service.impl.ComplexScoreFormServiceImpl;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 学生表单Controller
 * 
 * @author ruoyi
 * @date 2024-04-25
 */
@RestController
@RequestMapping("/complex/form")
@Tag(name = "【学生表单】管理")
public class ComplexScoreFormController extends BaseController {
    @Autowired
    private ComplexScoreFormServiceImpl complexScoreFormService;

    /**
     * 查询学生表单列表
     */
    @Operation(summary = "查询学生表单列表")
    @PreAuthorize("@ss.hasPermi('complex:form:list')")
    @GetMapping("/list")
    public TableDataInfo list(ComplexScoreForm complexScoreForm) {
        startPage();
        List<ComplexScoreForm> list = complexScoreFormService.selectComplexScoreFormList(complexScoreForm);
        return getDataTable(list);
    }

    @Anonymous
    @GetMapping("/test")
    public AjaxResult test(ComplexScoreForm scoreForm) {
        List<Map<ExcelColumn, String>> exportExcel = complexScoreFormService.exportExcelData(scoreForm);
        complexScoreFormService.handleExport(exportExcel);
        return success();
    }

    /**
     * 导出学生表单列表
     * 
     * @throws IOException
     */
    @Operation(summary = "导出学生表单列表")
    @PreAuthorize("@ss.hasPermi('complex:form:export')")
    @Log(title = "学生表单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ComplexScoreForm complexScoreForm) throws IOException {
        List<Map<ExcelColumn, String>> exportExcel = complexScoreFormService.exportExcelData(complexScoreForm);
        String fileUrl = complexScoreFormService.handleExport(exportExcel);
        FileUtils.writeBytes(fileUrl, response.getOutputStream());
    }

    /**
     * 获取学生表单详细信息
     */
    @Operation(summary = "获取学生表单详细信息")
    @PreAuthorize("@ss.hasPermi('complex:form:query')")
    @GetMapping(value = "/{scoreId}")
    public AjaxResult getInfo(@PathVariable("scoreId") Long scoreId) {
        return success(complexScoreFormService.selectComplexScoreFormByScoreId(scoreId));
    }

    /**
     * 新增学生表单
     */
    @Operation(summary = "新增学生表单")
    @PreAuthorize("@ss.hasPermi('complex:form:add')")
    @Log(title = "学生表单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ComplexScoreForm scoreForm) {

        ComplexScoreForm complexScoreForm = new ComplexScoreForm();
        complexScoreForm.setCommitFlag(FormStatus.UNHANDLE);
        complexScoreForm.setUserId(SecurityUtils.getUserId());
        complexScoreForm.setStudentNumber(SecurityUtils.getUsername());
        if (complexScoreFormService.selectComplexScoreFormList(complexScoreForm).size() > 0) {
            return error("已提交，无法再次提交");
        }
        complexScoreForm.setCommitFlag(FormStatus.COMMIT);
        if (complexScoreFormService.selectComplexScoreFormList(complexScoreForm).size() > 0) {
            return error("表单已通过，无法再次提交");
        }
        scoreForm.setStudentClass(SecurityUtils.getLoginUser().getUser().getStudentClass());
        return toAjax(complexScoreFormService.insertComplexScoreForm(scoreForm));
    }

    @PostMapping("/upload")
    @PreAuthorize("@ss.hasPermi('complex:form:add')")
    public AjaxResult uploadFile(MultipartFile file) throws IOException {
        String upload = FileUploadUtils.upload(file);
        return success(upload);
    }

    /**
     * 修改学生表单
     */
    @Operation(summary = "修改学生表单")
    @PreAuthorize("@ss.hasPermi('complex:form:edit')")
    @Log(title = "学生表单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComplexScoreForm complexScoreForm) {
        return toAjax(complexScoreFormService.updateComplexScoreForm(complexScoreForm));
    }

    @GetMapping("/commit")
    @PreAuthorize("@ss.hasPermi('complex:form:commit')")
    public AjaxResult commit(Long scoreId) {
        ComplexScoreForm scoreForm = new ComplexScoreForm();
        scoreForm.setScoreId(scoreId);
        scoreForm.setCommitFlag(FormStatus.COMMIT);
        return success(complexScoreFormService.updateComplexScoreForm(scoreForm));
    }

    @GetMapping("/refuse")
    @PreAuthorize("@ss.hasPermi('complex:form:refuse')")
    public AjaxResult refuse(Long scoreId) {
        ComplexScoreForm scoreForm = new ComplexScoreForm();
        scoreForm.setCommitFlag(FormStatus.REFUSE);
        scoreForm.setScoreId(scoreId);
        return success(complexScoreFormService.updateComplexScoreForm(scoreForm));
    }

    /*
     * @GetMapping("/admit")
     * 
     * @PreAuthorize("@ss.hasPermi('complex:form:admit')")
     * public AjaxResult admit(Long formId) {
     * ComplexScoreForm scoreForm = new ComplexScoreForm();
     * scoreForm.setScoreId(formId);
     * scoreForm.setCommitFlag(FormStatus.COMMIT);
     * return success(complexScoreFormService.updateComplexScoreForm(scoreForm));
     * }
     */
    /**
     * 删除学生表单
     */
    @Operation(summary = "删除学生表单")
    @PreAuthorize("@ss.hasPermi('complex:form:remove')")
    @Log(title = "学生表单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scoreIds}")
    public AjaxResult remove(@PathVariable(name = "scoreIds") Long[] scoreIds) {
        return toAjax(complexScoreFormService.deleteComplexScoreFormByScoreIds(scoreIds));
    }
}
